package com.zhangrui.dao;

import com.zhangrui.pojo.User;
import com.zhangrui.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName:UserDaoText
 * Package:com.zhangrui.dao
 * Description:
 *
 * @Date:2021/8/1113:24
 * @Author:zhangrui@qq.com
 */
//这是一个测试类，测试用mybatis框架连接数据库并操作数据库！！！1
public class UserDaoText {
    //这个是测试普通的select语句！！
    public static void main(String[] args) {
        //第一步，获取SqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //第二部，执行SQL
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = mapper.getUserList();
        for (User user : userList) {
            System.out.println(user);
        }
        //关闭sql
        sqlSession.close();
    }

    //测试通过查询条件去查询出数据！！！
    @Test
    public void getUserListId() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> userListId = userMapper.getUserListId(2);
        for (User user : userListId) {
            System.out.println(user);
        }
        sqlSession.close();
    }

    //测试通过查询条件去查询出数据！！！
    @Test
    public void getUserListName() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> listName = userMapper.getUserListName("张瑞");
        for (User user : listName) {
            System.out.println(user);
        }
        sqlSession.close();
    }

    //测试在数据库插入一条语句
    @Test
    public void insertData() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int insertData = userMapper.insertData(new User(4, "王小胖", "9999999999"));
        System.out.println(insertData);
        sqlSession.commit();
        sqlSession.close();
    }

    //测试在数据库修改一条或者多条数据！！！
    @Test
    public void updateData() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int updateData = userMapper.updateData(new User(1, "张瑞", "6666666666"));
        System.out.println(updateData);
        sqlSession.commit();
        sqlSession.close();
    }

    //测试删除一条数据或者多条数据！！！
    @Test
    public void deleteData() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int deleteData = userMapper.deleteData(new User(4, "王小胖", "9999999999"));
        System.out.println(deleteData);
        sqlSession.commit();
        sqlSession.close();
    }

    //通过万能的map键值对去测试查询数据
    @Test
    public void getUserMap() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("namemap", "邵春波");
        User userMap = userMapper.getUserMap(map);
        System.out.println(userMap);
        sqlSession.close();
    }

    //通过万能的map键值对测试插入数据库
    @Test
    public void insertUserMap() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        Map<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("idmap", 8);
        hashMap.put("namemap", "张小胖");
        hashMap.put("pwdmap", "3745862262");
        int insertDataMap = userMapper.insertDataMap(hashMap);
        System.out.println(insertDataMap);
        sqlSession.commit();
        sqlSession.close();
    }

    //通过万能的Map键值对修改数据库
    @Test
    public void updateUserMap() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        Map<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("pwdmap", "777777777");
        hashMap.put("namemap", "张小胖");
        int updateDataMap = userMapper.updateDataMap(hashMap);
        System.out.println(updateDataMap);
        sqlSession.commit();
        sqlSession.close();
    }
}
